package com.xiaoxx.sharding;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaoxx.model.entity.chat.ChatContent;
import com.xiaoxx.service.ChatContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Slf4j
@Order(value = 1) // 数字越小，越先执行
@Component
public class ShardingTablesLoadRunner implements CommandLineRunner {

    @Resource
    private ChatContentService chatContentService;

    @Override
    public void run(String... args) {
        // 读取已有分表，进行缓存
        LambdaQueryWrapper<ChatContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatContent::getCreateTime, LocalDateTime.now()).last("limit 1");
        chatContentService.list(queryWrapper);
        log.info(">>>>>>>>>> 【ShardingTablesLoadRunner】缓存已有分表成功 <<<<<<<<<<");
    }
}

